package com.sec.android.app.myfiles.feature.Hover;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.thumbnail.AudioThumbnailImp;
import com.sec.android.app.myfiles.thumbnail.ThumbnailImp;

import java.io.IOException;

public class AudioHoverView extends PlayableMediaHoverView implements OnAudioFocusChangeListener {
    private MediaPlayer mMediaPlayer;

    public AudioHoverView(Activity activity, AbsMyFilesFragment fragment, FileRecord record) {
        super(activity, fragment, record);
    }

    @Override
    protected void initViews() {
        super.initViews();
        mLayout = (LinearLayout) View.inflate(mContext, R.layout.hover_audio_view, null);

        if (mDialog != null && mLayout != null) {
            mDialog.setContentView(mLayout);
            setDataSource();
            setAudioView();
            initHoverButton();
            setDialogPosition((-1) * mView.getWidth() / 4, mView.getHeight() / 4);
            mLayout.setOnHoverListener(mLayoutListener);
        }
    }

    @Override
    protected void initHoverButton() {
        super.initHoverButton();
        if (mPlayButton != null) {
            mPlayButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.pause();
                        mPlayButton.setBackground(mContext.getDrawable(R.drawable.hover_play_btn));
                        mPlayButton.setContentDescription(mContext.getResources().getString(R.string.hover_play));
                    } else {
                        if (mIsStarted) {
                            mMediaPlayer.start();
                        } else {
                            playAudio();
                        }
                        mPlayButton.setBackground(mContext.getDrawable(R.drawable.hover_pause_btn));
                        mPlayButton.setContentDescription(mContext.getResources().getString(R.string.hover_pause));
                    }
                }
            });
        }
    }

    private void setAudioView() {

        ImageView previewThumbnail = (ImageView) mLayout.findViewById(R.id.airview_preview_audio);
        previewThumbnail.setImageResource(MediaFile.getIcon(mContext, mRecord));

        AudioThumbnailImp thumbNail = new AudioThumbnailImp(mContext);
        Bitmap bmp = thumbNail._createThumbnail(mRecord, ThumbnailImp.THUMBNAIL_SIZE);
        if (bmp != null) {
            previewThumbnail.setImageBitmap(bmp);

            float thumbnailWidth = bmp.getWidth();
            float thumbnailHeight = bmp.getHeight();
            float ratio = getRatio(thumbnailWidth, thumbnailHeight);

            android.view.ViewGroup.LayoutParams params = previewThumbnail.getLayoutParams();
            params.width = (int) (thumbnailWidth * ratio);
            params.height = (int) (thumbnailHeight * ratio);
            previewThumbnail.setLayoutParams(params);
        }
    }

    private void setDataSource() {
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(mRecord.getFullPath());
            mMediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
            Log.e(this, "IllegalArgumentException:" + e.toString());
        } catch (SecurityException e) {
            Log.e(this, "SecurityException:" + e.toString());
        } catch (IllegalStateException e) {
            Log.e(this, "IllegalStateException:" + e.toString());
        } catch (IOException e) {
            Log.e(this, "IOException:" + e.toString());
        }
    }

    @Override
    public void dismissPopup() {
        super.dismissPopup();
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
                audioManager.abandonAudioFocus(this);
                mIsStarted = false;
                mMediaPlayer.stop();
            }
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

    private void playAudio() {
        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT) == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mIsStarted = true;
            mMediaPlayer.start();
        } else {
            Log.e(this, "start failed - audioFocus request is not granted");
        }
    }

    @Override
    public void onAudioFocusChange(int focusChange) {
        switch (focusChange) {
            case AudioManager.AUDIOFOCUS_LOSS:
            case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                if (mMediaPlayer != null) {
                    if (mMediaPlayer.isPlaying()) {
                        mIsStarted = false;
                        mMediaPlayer.stop();
                        mMediaPlayer.release();
                    }
                    mMediaPlayer = null;
                }
                break;
            case AudioManager.AUDIOFOCUS_GAIN:
                break;
            default:
                break;
        }
    }


}
